<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>【学习记录】【Python】【cv2】自学cv2 的简单记录（已经腰斩换新的）.md | 蓝湖畔淅淅沥沥的雨</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="说明 - 2022-05-05 本篇博客为本人原创, 原发布于CSDN, 在搭建个人博客后使用爬虫批量爬取并挂到个人博客, 出于一些技术原因博客未能完全还原到初始版本(而且我懒得修改), 在观看体验上会有一些瑕疵 ,若有需求会发布重制版总结性新博客。发布时间统一定为1111年11月11日。钦此。  1.图像的载入、显示、保存 ​ ​    import cv2 ​ #读入图像 cv2.imrea">
<meta property="og:type" content="article">
<meta property="og:title" content="【学习记录】【Python】【cv2】自学cv2 的简单记录（已经腰斩换新的）.md">
<meta property="og:url" content="http://example.com/1111/11/11/%E3%80%90%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E3%80%91%E3%80%90Python%E3%80%91%E3%80%90cv2%E3%80%91%E8%87%AA%E5%AD%A6cv2%20%E7%9A%84%E7%AE%80%E5%8D%95%E8%AE%B0%E5%BD%95%EF%BC%88%E5%B7%B2%E7%BB%8F%E8%85%B0%E6%96%A9%E6%8D%A2%E6%96%B0%E7%9A%84%EF%BC%89/index.html">
<meta property="og:site_name" content="蓝湖畔淅淅沥沥的雨">
<meta property="og:description" content="说明 - 2022-05-05 本篇博客为本人原创, 原发布于CSDN, 在搭建个人博客后使用爬虫批量爬取并挂到个人博客, 出于一些技术原因博客未能完全还原到初始版本(而且我懒得修改), 在观看体验上会有一些瑕疵 ,若有需求会发布重制版总结性新博客。发布时间统一定为1111年11月11日。钦此。  1.图像的载入、显示、保存 ​ ​    import cv2 ​ #读入图像 cv2.imrea">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="1111-11-11T03:06:11.000Z">
<meta property="article:modified_time" content="2022-05-06T08:13:02.772Z">
<meta property="article:author" content="StarsWhisper">
<meta property="article:tag" content="OldBlog(Before20220505)">
<meta property="article:tag" content="python库">
<meta property="article:tag" content="cv">
<meta property="article:tag" content="图像处理">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="蓝湖畔淅淅沥沥的雨" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

  
<link rel="stylesheet" href="/plugin/bganimation/bg.css">

  

  <link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css" rel="stylesheet" type="text/css">
<meta name="generator" content="Hexo 6.1.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <div class="widget-wrap mobile-header">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

        <section id="main"><article id="post-【学习记录】【Python】【cv2】自学cv2 的简单记录（已经腰斩换新的）" class="wow slideInRight article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/1111/11/11/%E3%80%90%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E3%80%91%E3%80%90Python%E3%80%91%E3%80%90cv2%E3%80%91%E8%87%AA%E5%AD%A6cv2%20%E7%9A%84%E7%AE%80%E5%8D%95%E8%AE%B0%E5%BD%95%EF%BC%88%E5%B7%B2%E7%BB%8F%E8%85%B0%E6%96%A9%E6%8D%A2%E6%96%B0%E7%9A%84%EF%BC%89/" class="article-date">
  <time class="post-time" datetime="1111-11-11T03:06:11.000Z" itemprop="datePublished">
    <span class="post-month">11月</span><br/>
    <span class="post-day">11</span>
  </time>
</a>
   
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      【学习记录】【Python】【cv2】自学cv2 的简单记录（已经腰斩换新的）.md
    </h1>
  

        <div>
          
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">基础知识</a>,<a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/python/">python</a>,<a class="article-category-link" href="/categories/%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/python/opencv/">opencv</a>
  </div>

          
              

          
        </div>
      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="说明-2022-05-05"><a class="markdownIt-Anchor" href="#说明-2022-05-05"></a> 说明 - 2022-05-05</h2>
<p>本篇博客为本人原创, 原发布于CSDN, 在搭建个人博客后使用爬虫批量爬取并挂到个人博客, 出于一些技术原因博客未能完全还原到初始版本(而且我懒得修改), 在观看体验上会有一些瑕疵 ,若有需求会发布重制版总结性新博客。发布时间统一定为1111年11月11日。钦此。</p>
<h2 id="1图像的载入-显示-保存"><a class="markdownIt-Anchor" href="#1图像的载入-显示-保存"></a> 1.图像的载入、显示、保存</h2>
<p>​<br />
​    import cv2<br />
​<br />
#读入图像 cv2.imread(filepath,flags)<br />
#flags参数的取值：<br />
#cv2.IMREAD_COLOR：默认，载入一个彩色图像，忽略透明度   可用1代替<br />
#cv2.IMREAD_GRAYSCALE：载入一个灰阶图像  可用0代替<br />
#cv2.IMREAD_UNCHANGED：载入一个包含 Alpha 通道（透明度）的图像   可用-1代替<br />
img1=cv2.imread(‘img_chess.jpg’,0)</p>
<pre><code>#显示图像   cv2.imshow(wname,img)
#wname  窗口的名字 window name
#img 要显示的图像 窗口他大小为自动调整图片大小
cv2.imshow('image_one',img1)
key=cv2.waitKey(0)                  #等待键盘输入，单位毫秒，0为无限等待  没有这句话窗口只会闪一下就消失
if key==27:
    print('您按了ESC')
#cv2.destroyAllWindows()            #销毁所有窗口
cv2.destroyWindow('image_one')      #指定窗口名字销毁窗口
</code></pre>
<p>​<br />
​    #保存图像 cv2.imwrite(file，img，num)<br />
​    #file 文件名<br />
​    #img  要保存的图像<br />
​    #num 对于JPEG，其表示的是图像的质量，用0 - 100的整数表示，默认95;对于png 用0-9 ,第三个参数表示的是压缩级别。默认为3.<br />
​    cv2.imwrite(‘img_chess_gray.jpg’,img1,[cv2.IMWRITE_JPEG_QUALITY,0])<br />
​    cv2.imwrite(‘img_chess_gray.png’,img1,[cv2.IMWRITE_PNG_COMPRESSION,0])<br />
​    # jpg属于有损压缩，是以图片的清晰度为代价的，数字越小，压缩比越高，图片质量损失越严重<br />
​    # png属于无损压缩，数字0-9，数字越低，压缩比越低<br />
​<br />
input(‘按回车退出程序’)</p>
<p>那张保存为jpg格式的图片惨不忍睹</p>
<h2 id="2简单的图片操作-反转-复制-缩放-裁剪"><a class="markdownIt-Anchor" href="#2简单的图片操作-反转-复制-缩放-裁剪"></a> 2.简单的图片操作 反转、复制、缩放、裁剪</h2>
<p>flip copy resize</p>
<p>​<br />
​    import cv2<br />
​<br />
img1=cv2.imread(‘img_chess.jpg’)</p>
<pre><code>#图片反转 cv2.flip(img,flipcode)
#flipcode &gt;0 沿x轴 flipcode=0 沿y轴 flipcode&lt;0 xy轴同时反转
img1_flipx=cv2.flip(img1,1)
img1_flipy=cv2.flip(img1,0)
img1_flipxy=cv2.flip(img1,-1)

cv2.imshow('1fx',img1_flipx)
cv2.imshow('1fy',img1_flipy)
cv2.imshow('1fxy',img1_flipxy)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>
<p>​<br />
​    #图像复制<br />
​    img1_cp=img1.copy()<br />
​    cv2.imshow(‘1cp’,img1_cp)<br />
​    cv2.waitKey()<br />
​    cv2.destroyAllWindows()</p>
<p>​<br />
​    #图像的缩放<br />
​    img_info=img1.shape<br />
​    height=img_info[0]<br />
​    width=img_info[1]<br />
​    img1_resized=cv2.resize(img1.copy(),(int(width<em>1.2),int(height</em>0.6)))<br />
​    cv2.imshow(‘1resized’,img1_resized)<br />
​    cv2.waitKey(0)<br />
​    cv2.destroyAllWindows()<br />
​<br />
#图像的裁剪<br />
img1_cut=img1[350:800,60:500]<br />
h,w,n=img1.shape<br />
img1_cut2=img1[int(h/4):int(h<em>3/4),int(w/4):int(w</em>3/4)]<br />
cv2.imshow(‘1cut’,img1_cut)<br />
cv2.imshow(‘1cut2’,img1_cut2)<br />
cv2.waitKey(0)<br />
cv2.destroyAllWindows()</p>
<h2 id="3图像通道的分离与合成"><a class="markdownIt-Anchor" href="#3图像通道的分离与合成"></a> 3.图像通道的分离与合成</h2>
<p><strong>分离：</strong><br />
img1B,img1G,img1R=np.dsplit(img1,3)<br />
img1B,img1G,img1R=cv2.split(img1)都可以<br />
<strong>合成：</strong><br />
cv2.merge([b,g,r])<br />
cv2.merge([zeros, zeros, img1R])</p>
<p><strong>注意倒数第五句的注释</strong></p>
<p>​<br />
​    import cv2<br />
​    import numpy as np<br />
​<br />
def showone(img):<br />
cv2.imshow(‘show image’,img)<br />
cv2.waitKey(0)<br />
cv2.destroyAllWindows()<br />
def showone_color(img,color):<br />
zeros=np.zeros(img.shape[:2],dtype=‘uint8’) #记得调整格式<br />
if color == ‘B’:<br />
cv2.imshow(‘blue’,cv2.merge([img1B,zeros,zeros]))<br />
elif color == ‘G’:<br />
cv2.imshow(‘green’, cv2.merge([zeros, img1G, zeros]))<br />
elif color == ‘R’:<br />
cv2.imshow(‘red’, cv2.merge([zeros, zeros, img1R]))<br />
cv2.waitKey(0)<br />
cv2.destroyAllWindows()<br />
def showone_merge(b,g,r):<br />
cv2.imshow(‘merge’,cv2.merge([b,g,r]))<br />
cv2.waitKey(0)<br />
cv2.destroyAllWindows()</p>
<p>​<br />
​    img1=cv2.imread(‘theimg.png’,1)<br />
​<br />
img1B,img1G,img1R=np.dsplit(img1,3)<br />
#img1B,img1G,img1R=cv2.split(img1)</p>
<pre><code>print(img1B.dtype)          #矩阵元素的格式为uint8
</code></pre>
<p>​<br />
​<br />
​    showone(img1B)      #这里图像为灰色，原因是当调用cv2.imshow的时候G、R两个通道被默认设为了与B通道一样，三通道一样时显示灰度图</p>
<p>​<br />
​    showone_color(img1B,‘B’)<br />
​    showone_color(img1G,‘G’)<br />
​    showone_color(img1R,‘R’)<br />
​<br />
showone_merge(img1B,img1G,img1R)        #合成后是原图</p>
<p>​</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/1111/11/11/%E3%80%90%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E3%80%91%E3%80%90Python%E3%80%91%E3%80%90cv2%E3%80%91%E8%87%AA%E5%AD%A6cv2%20%E7%9A%84%E7%AE%80%E5%8D%95%E8%AE%B0%E5%BD%95%EF%BC%88%E5%B7%B2%E7%BB%8F%E8%85%B0%E6%96%A9%E6%8D%A2%E6%96%B0%E7%9A%84%EF%BC%89/" data-id="cl2uhoecd001xe4j31crt0w9n" class="article-share-link">分享</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/OldBlog-Before20220505/" rel="tag">OldBlog(Before20220505)</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/cv/" rel="tag">cv</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python%E5%BA%93/" rel="tag">python库</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86/" rel="tag">图像处理</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/1111/11/11/%E3%80%90%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E3%80%91%E3%80%90Python%E3%80%91%E3%80%90numpy%E3%80%91%E8%87%AA%E5%AD%A6numpy%E5%BA%93%E7%9A%84%E7%AE%80%E8%A6%81%E8%AE%B0%E5%BD%95%E5%B8%96%EF%BC%88%E5%B7%B2%E7%BB%8F%E8%85%B0%E6%96%A9%E6%8D%A2%E6%96%B0%E7%9A%84%EF%BC%89/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          【学习记录】【Python】【numpy】自学numpy库的简要记录帖（已经腰斩换新的）.md
        
      </div>
    </a>
  
  
    <a href="/1111/11/11/%E3%80%90%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E3%80%91%E3%80%90Python%E3%80%91%E3%80%90numpy%E3%80%91%E8%87%AA%E5%AD%A6%E7%AE%80%E8%A6%81%E8%AE%B0%E5%BD%95%EF%BC%88%E6%9C%AA%E5%AE%8C%EF%BC%89/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">【学习记录】【Python】【numpy】自学简要记录（未完）.md</div>
    </a>
  
</nav>

  
</article>



</section>
        
          <aside id="sidebar">
  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <h1 class="blog-title">蓝湖畔淅淅沥沥的雨</h1>
    <h2 class="blog-subtitle">All tragedy erased, I see only wonders...</h2>
    <ul class="blog-link">
     
          <a href="/" title="Home">
            <li>主页</li>
          </a>
        
          <a href="/archives" title="Archives">
            <li>归档</li>
          </a>
        
          <a href="/categories" title="Categories">
            <li>分类</li>
          </a>
        
          <a href="/tags" title="Tags">
            <li>标签</li>
          </a>
        
          <a href="/knightabout" title="Knightabout">
            <li>关于</li>
          </a>
        
          <a href="/bridges" title="Bridges">
            <li>传送门</li>
          </a>
        
          <a href="/announcement" title="Announcement">
            <li>公告</li>
          </a>
        
    </ul>
  </div>
</div>

  
    <div class="widget-wrap">
  <h3 class="widget-title"></h3>
  <div class="widget">
    <img class="avatar" src="/images/avatar.png">
    <h2 class="author">StarsWhisper</h2>
    <h3 class="description"></h3>
    <div class="count-box">
      <a href="/archives"><div><strong>75</strong><br>文章</div></a>
      <a href="/categories"><div><strong>31</strong><br>分类</div></a>
      <a href="/tags"><div><strong>56</strong><br>标签</div></a>
    </div>



    <div class="social-link">
      
        <a class="hvr-bounce-in" href="https://github.com/Wldcmzy" target="_blank" title="Github">
          Github
        </a>
      
        <a class="hvr-bounce-in" href="https://blog.csdn.net/wldcmzy" target="_blank" title="CSDN">
          CSDN
        </a>
      
        <a class="hvr-bounce-in" href="https://space.bilibili.com/83743701" target="_blank" title="bilibili(无技术和学习内容)">
          bilibili(无技术和学习内容)
        </a>
      
    </div>

    <div class="friend-link">
      <h2>友情链接</h2>
      
        <a class="hvr-bounce-in" href="https://shanamaid.github.io/" target="_blank" title="夏娜主题作者的博客">
          夏娜主题作者的博客
        </a>
      
    </div>
  </div>
</div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy;2021 - 2022 StarsWhisper<br>
      由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动 | 
      主题-<a target="_blank" rel="noopener" href="https://github.com/ShanaMaid/hexo-theme-shana">Shana</a>(但是魔改)
      
    </div>
    
  </div>
</footer>
    </div>
    

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//apps.bdimg.com/libs/wow/0.1.6/wow.min.js"></script>
<script>
new WOW().init();
</script>   


  
<link rel="stylesheet" href="/plugin/fancybox/jquery.fancybox.css">

  
<script src="/plugin/fancybox/jquery.fancybox.pack.js"></script>




  
<link rel="stylesheet" href="/plugin/galmenu/GalMenu.css">

  
<script src="/plugin/galmenu/GalMenu.js"></script>

  <div class="GalMenu GalDropDown">
      <div class="circle" id="gal">
        <div class="ring">
          
            <a href="/announcement" title="" class="menuItem">公告</a>
          
            <a href="/tags" title="" class="menuItem">标签</a>
          
            <a href="/categories" title="" class="menuItem">分类</a>
          
            <a href="/archives" title="" class="menuItem">归档</a>
          
            <a href="/knightabout" title="" class="menuItem">关于</a>
          
            <a href="/bridges" title="" class="menuItem">传送门</a>
          
        </div>
        
          <audio id="audio" src="#"></audio>
        
      </div> 
</div>
<div id="overlay" style="opacity: 1; cursor: pointer;"></div>
  <script type="text/javascript">var items = document.querySelectorAll('.menuItem');
    for (var i = 0,
    l = items.length; i < l; i++) {
      items[i].style.left = (50 - 35 * Math.cos( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%";
      items[i].style.top = (50 + 35 * Math.sin( - 0.5 * Math.PI - 2 * (1 / l) * i * Math.PI)).toFixed(4) + "%"
    }</script>
<script type="text/javascript">
  $(document).ready(function() {
    $('body').GalMenu({
      'menu': 'GalDropDown'
    })
  });
</script>

  <section class="hidden-xs"> 
  <ul class="cb-slideshow"> 
    <li><span>苟利</span></li> 
    <li><span>国家</span></li> 
    <li><span>生死以</span></li> 
    <li><span>岂能</span></li> 
    <li><span>祸福</span></li> 
    <li><span>趋避之</span></li> 
  </ul>
</section>

<script src="/js/script.js"></script>




  </div>
</body>
</html>